package org.xtown.bingluo.xtieba.db;

import org.xtown.bingluo.xtieba.model.XtiebaPosts;
import org.xtown.bingluo.xtieba.utils.LogUtlils;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class XtiebaPostsDb {
	// 拿到SQLiteDatabase对象
	private DBHelper dbhelp;
	private String TB_NAME;
	private static XtiebaPostsDb mXtiebaPostsDbInstance;
	public static String ID = "_id";
	public static String KZID = "kzid";
	public static String TITLE = "title";
	public static String AUTHOR = "author";
	public static String POSTS_CREATED_AT = "posts_created_at";
	public static String CREATED_AT = "created_at";
	public static String UPDATED_AT = "updated_at";

	private XtiebaPostsDb(Context context) {
		this.dbhelp = new DBHelper(context);
		this.TB_NAME = DBInfo.Table.POSTS_TB_NAME;
	}
	
	public static XtiebaPostsDb getNetInstance(Context c) {
        synchronized (XtiebaPostsDb.class) {
        	XtiebaPostsDb m = mXtiebaPostsDbInstance;
            if (m == null) {
                m = new XtiebaPostsDb(c);
                mXtiebaPostsDbInstance = m;
            }
            m = mXtiebaPostsDbInstance;
            return m;
        }
	}

	// 保存
	public XtiebaPosts save(XtiebaPosts o) {
		XtiebaPosts tmp = selectByKzid(o.kzid);
		if (tmp == null) {
			if (o.id == 0) {
				//添加
				o = insert(o);
			} else {
				//更新
				update(o);
			}
		} else {
			o.id = tmp.id;
		}
		return o;
	}

	//随后就是增删改查的方法

	public XtiebaPosts selectByKzid(String kzid) {
        SQLiteDatabase db = dbhelp.getReadableDatabase();
        Cursor cur = db.query(TB_NAME, new String[] {"*"}, KZID + " = " + kzid, null, null, null, null);
        XtiebaPosts o = null;
        if (cur != null) {
        	while (cur.moveToNext()) {
        		o = makeXtiebaPosts(cur);
        		break;
    		}
        }
        return o;
	}

	private XtiebaPosts makeXtiebaPosts(Cursor cur) {
		XtiebaPosts o;
		o = new XtiebaPosts(
			cur.getInt(cur.getColumnIndex(ID)),
			cur.getString(cur.getColumnIndex(KZID)),
			cur.getString(cur.getColumnIndex(TITLE)),
			cur.getString(cur.getColumnIndex(AUTHOR)),
			cur.getInt(cur.getColumnIndex(POSTS_CREATED_AT)),
			cur.getInt(cur.getColumnIndex(CREATED_AT)),
			cur.getInt(cur.getColumnIndex(UPDATED_AT))
		);
		return o;
	}

	public XtiebaPosts insert(XtiebaPosts o){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        ContentValues cv = makeContentValues(o);
        int id = (int)db.insert(TB_NAME, null, cv);
        o.id = id;
        return o;
	}

	public int delete(int id){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        String where = ID + "=?";
        String[] whereValue = {Integer.toString(id)};
        return db.delete(TB_NAME, where, whereValue);
	}

	public int update(XtiebaPosts o){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        String where = ID + "=?";
        String[] whereValue = {Integer.toString(o.id)};
        ContentValues cv = makeContentValues(o);
        return db.update(TB_NAME, cv, where, whereValue);
	}

	private ContentValues makeContentValues(XtiebaPosts o) {
		ContentValues cv = new ContentValues();
        cv.put(KZID, o.kzid);
        cv.put(TITLE, o.title);
        cv.put(AUTHOR, o.author);
        cv.put(POSTS_CREATED_AT, o.posts_created_at);
        cv.put(CREATED_AT, o.created_at);
        cv.put(UPDATED_AT, o.updated_at);
		return cv;
	}
	
	//limit offset,length
	public Cursor getListCursor(int offet, int length){
		SQLiteDatabase dbopen = dbhelp.getReadableDatabase();
		Cursor cursor = dbopen.rawQuery("SELECT * FROM " + DBInfo.Table.POSTS_TB_NAME + " GROUP BY " + KZID + " LIMIT " + offet + "," + length, null);
		return cursor;
	}
	
	public void deleteAll(){
    	SQLiteDatabase dbopen = dbhelp.getReadableDatabase();  
    	String sql1 = "DELETE FROM " + DBInfo.Table.POSTS_TB_NAME;    
    	String sql2 = "SELECT * FROM sqlite_sequence";    
    	String sql3 = "UPDATE sqlite_sequence SET seq=0 WHERE name='" + DBInfo.Table.POSTS_TB_NAME + "'";
        dbopen.execSQL(sql1);
        dbopen.rawQuery(sql2, null);
        dbopen.execSQL(sql3);
    }
	
	public int deleteByKzid(String kzid){
        SQLiteDatabase db = dbhelp.getWritableDatabase();
        String where = KZID + "=?";
        String[] whereValue = {kzid};
        return db.delete(TB_NAME, where, whereValue);
	}
	
	/* 
     * get the count of the database 
     */  
    public int getCount(){  
        SQLiteDatabase dbopen = dbhelp.getReadableDatabase();  
        Cursor cursor = dbopen.rawQuery("SELECT COUNT(*) FROM " + DBInfo.Table.POSTS_TB_NAME, null);  
        cursor.moveToNext();
        int r = cursor.getInt(0);
        LogUtlils.println("getCount="+r);
        cursor.close();
        return r;
    }
	
	public void close() {
		this.dbhelp.close();
	}
}